define(['jquery', 'bootstrap', 'backend', 'table', 'form','code'], function ($, undefined, Backend, Table, Form,Code,undefined) {
    // var Form = {
    //     config: {
    //         fieldlisttpl: '<dd class="form-inline">' +
    //             // '<input type="text" name="<%=name%>[<%=index%>][key]" class="form-control" value="<%=key%>" placeholder="<%=options.keyPlaceholder||\'\'%>" size="10" /> ' +
    //             '<input type="text" name="<%=name%>[<%=index%>][value]" class="form-control" value="<%=value%>" placeholder="<%=options.valuePlaceholder||\'\'%>" /> ' +
    //             // '<span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> ' +
    //             '<span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span>' +
    //             '</dd>'
    //     }
    // };
    Form.config.fieldlisttpl = '<dd class="form-inline">' +
        '<input type="text" name="<%=name%>[<%=index%>][key]" class="form-control" value="<%=index+1%>" disabled placeholder="<%=options.keyPlaceholder||\'\'%>" size="10" /> ' +
        '<input type="text" name="<%=name%>[<%=index%>][value]" class="form-control" value="<%=value%>" placeholder="<%=options.valuePlaceholder||\'\'%>" /> ' +
        '<span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> ' +
        '<span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span>' +
        '</dd>';


    var Controller = {
        index: function () {
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "短链快捷-搜索";};
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'short/chain/config/index' + location.search,
                    add_url: 'short/chain/config/add',
                    edit_url: 'short/chain/config/edit',
                    batch_url: 'short/chain/config/batch',
                    del_url: 'short/chain/config/del',
                    multi_url: 'short/chain/config/multi',
                    import_url: 'short/chain/config/import',
                    table: 'short_chain_config',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false,sortable: true},
                        {field: 'title', title: __('短链信息'), operate: "like",sortable: true},
                        // {field: 'group_id', title: __('Group_id'), operate: false},
                        {
                            field: 'group_id',
                            title: __('Group_id'),
                            visible: true,
                            addClass: "selectpage",
                            operate: 'like',
                            extend: "data-source='short/chain/group' data-field='name' data-primary-key='id'"
                        },
                        {field: 'domain.domain', title: __('Domain'), visible: true, addClass: "selectpage", extend: "data-source='short/chain/domain' data-field='domain' data-primary-key='domain_id'"},
                        {
                            field: 'orginal_url',
                            title: __('Orginal_url'),
                            operate: 'LIKE',
                            cellStyle:function (value,row,index,field) {
                                    return {
                                        css: {
                                            "min-width": "150px",
                                            "white-space": "nowrap",
                                            "text-overflow": "ellipsis",
                                            "overflow": "hidden",
                                            // "color": "red",
                                            "max-width":"300px"
                                        }
                                    };
                            },
                            // editable: true
                        },
                        {
                            field: 'short_url',
                            title: __('Short_url'),
                            operate: 'LIKE',
                            // editable: true
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,sortable: true},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,sortable: true},
                        {
                            field: 'status',
                            title: '状态',
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle,
                            sortable: true,
                            searchList: {0: __('关'), 1: __('开')}
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 批量修改按钮事件
            $(document).on('click', '.btn-batch', function () {
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Toastr.error(__('未选择任何记录'));
                    return;
                }
                Fast.api.open($.fn.bootstrapTable.defaults.extend.batch_url + '?ids=' + ids.join(','), __('批量修改'), {
                    area: ['600px', '400px'],
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        batch: function () {
            Controller.api.bindevent();
            // 监听type选择变化
            $(document).on('change', 'input[name="row[type]"]', function() {
                var type = $(this).val();
                if (type == 1) {
                    $('.title').hide();
                    $('.c_title').hide();
                    $('.url').show();
                } else if (type == 2) {
                    $('.title').show();
                    $('.c_title').hide();
                    $('.url').hide();
                } else if (type == 3) {
                    $('.title').hide();
                    $('.c_title').show();
                    $('.url').hide();
                }
            });

            // 初始化显示状态
            setTimeout(function() {
                var defaultType = $('input[name="row[type]"]:checked').val();
                if (defaultType == 1) {
                    $('.title').hide();
                    $('.url').show();
                } else if (defaultType == 2) {
                    $('.title').show();
                    $('.url').hide();
                }
            }, 100);
        },
        api: {
            bindevent: function () {
                var code = get_code();
                var short_url = $("#c-short_url").val();
                $(document).on("blur", "input[type=text][name='row[orginal_url][0][value]']",
                    function(){
                        console.log(55);
                        var domain_value = $("#c-domain_id option:selected").text();
                        if(!short_url){
                            var input_value = $("input[type=text][name='row[orginal_url][0][value]']").val();
                            $("#c-key").val(code);
                            if(input_value){
                                $("#c-short_url").val('http://'+domain_value+'/'+ code);
                            }
                        }

                    });
                $(document).on("change", "#c-domain_id",
                    function(){
                        var input_value = $("input[type=text][name='row[orginal_url][0][value]']").val();
                        var domain_value = $("#c-domain_id option:selected").text();
                        var key_value = $("#c-key").val();
                        if(!key_value){
                            $("#c-key").val(code);
                        }else{
                            code = key_value;
                        }
                        if(input_value){
                            $("#c-short_url").val('http://'+domain_value+'/'+ code);
                        }
                    });
                $(document).on("blur", "#c-key",
                    function(){
                        var input_value = $("#c-orginal_url").val();
                        var domain_value = $("#c-domain_id option:selected").text();
                        if(input_value){
                            $("#c-short_url").val('http://'+domain_value+'/'+ $(this).val());
                        }

                    });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
